package com.zelma.consumer.controller;

import java.util.Arrays;
import java.util.List;
import java.util.List;
import java.util.Map;
import java.util.Set;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zelma.consumer.util.JwtUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.zelma.consumer.entity.UserEntity;
import com.zelma.consumer.service.UserService;
import com.zelma.common.utils.PageUtils;
import com.zelma.common.utils.R;



/**
 * 
 *
 * @author zelma
 * @email 1239115389@qq.com
 * @date 2022-01-06 17:00:48
 */
@RestController
@RequestMapping("consumer/user")
public class UserController {
    @Autowired
    JwtUtil jwtUtil;

    @Autowired
    UserService userService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    StringRedisTemplate redisTemplate;
    //用户管理页面
    @GetMapping("/list")
    public List <UserEntity> list(){
        return userService.list();
    }

    @RequestMapping("/findUserByAll")
    public UserEntity findUserByAll (@RequestParam("all")String all){

        UserEntity userEntity = userService.getOne(new QueryWrapper <UserEntity>().eq("user_tel", all));

        if (userEntity!=null){
            return userEntity;
        }else{
            UserEntity userEntity1 = userService.getOne(new QueryWrapper <UserEntity>().eq("user_name", all));
            if (userEntity1!=null){
                return userEntity1;
            }
        }
        return null;
    }


    //用户登录接口
    @PostMapping("/Login")
    public R login(@RequestParam("authCode") String authCode,@RequestBody UserEntity userEntity){
        UserEntity userPhone = userService.getOne(new QueryWrapper<UserEntity>().eq("user_tel", userEntity.getUserTel()));
        if (userPhone != null && userEntity.getUserName().equals(userPhone.getUserName()))
        {
            String s = redisTemplate.opsForValue().get("UserAuthCode");
            if (authCode != null){
                if (authCode.equals(s)){
                    String token = jwtUtil.createJWT(userEntity.getUserName(), userEntity.getUserName(), "user");
                    redisTemplate.opsForValue().set("name",userEntity.getUserName());
                    redisTemplate.opsForValue().set("status","user");
                    BoundListOperations <String, String> users = redisTemplate.boundListOps("users");
                    users.leftPush(userEntity.getUserName());
                    return R.ok("登陆成功").put("token",token);
                }
                return R.error(500,"验证码输入有误");
            }
            return R.error(404,"请输入验证码");
        }
        return R.error(404,"您输入的手机号不正确");
    }

    //经纪人发送验证码接口
    @GetMapping("/authCode/{userPhone}")
    public R authCode(@PathVariable("userPhone") String userPhone){
        //点击发送验证码，使用MQ异步发送验证码
        rabbitTemplate.convertAndSend("1904",userPhone);
        return R.ok();
    }
}
